/**
 * Home Activity with six button:Track, Location, Scan, Pickup, Log In/Member and About
 * @author Junshuang Yang
 *
 */
package com.cats.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import com.cats.R;
import com.cats.utility.AccountManageUtility;

public class HomeActivity extends BaseActivity {
	
	public final static int REQUEST_CODE_SCAN = 15;
	public final static int REQUEST_CODE_LOGIN = 31;
	public final static int REQUEST_CODE_LOGIN_PICKUP = 63;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_home);
		System.out.println("Home created");
		getActionBar().hide();
		
		// TODO : finish action behavior
		View.OnClickListener onClickListener = new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				switch(v.getId()) {
				case (R.id.button1):
				{
					System.out.println("[HomeActivity]:Track selected");	
					final Intent intent = new Intent(HomeActivity.this, TrackActivity.class);
				    startActivity(intent);
				}
					break;
				case (R.id.button2):
				{
					System.out.println("[HomeActivity]:Scan selected");	
					final Intent intent = new Intent(HomeActivity.this, ScanActivity.class);
					startActivityForResult(intent, REQUEST_CODE_SCAN);
				}
					break;
				case (R.id.button3):
				{
					System.out.println("[HomeActivity]:Location selected");	
					final Intent intent = new Intent(HomeActivity.this, LocationActivity.class);
				    startActivity(intent);					
				}
					break;
				case (R.id.button4):
				{
					System.out.println("[HomeActivity]:Pickup selected");	
					if (AccountManageUtility.getSharedAccountManager(getApplicationContext()).isUserLogin()) {
						final Intent intent = new Intent(HomeActivity.this, PickupActivity.class);
					    startActivity(intent);
					}
					else {
						/**
						 * Haven't logged in
						 * Start a model activity for login result
						 * @see onActivityResult
						 */
						// for test
						//AccountManageUtility.getSharedAccountManager(getApplicationContext()).doLogIn("test", "123456");
						final Intent intent = new Intent(HomeActivity.this, LoginActivity.class);
						startActivityForResult(intent, REQUEST_CODE_LOGIN_PICKUP);
					}
				}
					break;
				case (R.id.button5):
				{
					/**
					 * Already logged in
					 */
					System.out.println("[HomeActivity]:Login/Member selected");
					if (AccountManageUtility.getSharedAccountManager(getApplicationContext()).isUserLogin()) {
						final Intent intent = new Intent(HomeActivity.this, MemberActivity.class);
					    startActivity(intent);												
					}
					else {
						/**
						 * Haven't logged in
						 * Start a model activity for login result
						 * @see onActivityResult
						 */
						// for test
						//AccountManageUtility.getSharedAccountManager(getApplicationContext()).doLogIn("test", "123456");
						final Intent intent = new Intent(HomeActivity.this, LoginActivity.class);
						startActivityForResult(intent, REQUEST_CODE_LOGIN);
					}
				}
					break;
				case (R.id.button6):
				{
					System.out.println("[HomeActivity]:About selected");	
					final Intent intent = new Intent(HomeActivity.this, AboutActivity.class);
				    startActivity(intent);					
				}
					break;
				default:
					break;
				}
				
			}
		};
			
		// TODO: add other button callback
		final Button btnTrack = (Button) findViewById(R.id.button1);
		btnTrack.setOnClickListener(onClickListener);

		final Button btnScan = (Button) findViewById(R.id.button2);
		btnScan.setOnClickListener(onClickListener);

		final Button btnLocation = (Button) findViewById(R.id.button3);
		btnLocation.setOnClickListener(onClickListener);			
		

		final Button btnPickup = (Button) findViewById(R.id.button4);
		btnPickup.setOnClickListener(onClickListener);

		final Button btnLogin = (Button) findViewById(R.id.button5);
		btnLogin.setOnClickListener(onClickListener);

		final Button btnAbout = (Button) findViewById(R.id.button6);
		btnAbout.setOnClickListener(onClickListener);
	};
	
	
	@Override
	// get scanned number
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (requestCode == REQUEST_CODE_SCAN && resultCode == RESULT_OK){
			/**
			 * success get scanned id, go to shipment page
			 */
			Bundle b = data.getExtras();
			String barcodeString = b.getString(getResources().getString(R.string.barcode));
			
			// navigate to shipment detail page
			final Intent intent = new Intent(HomeActivity.this, ShipmentActivity.class);
			intent.putExtra("ID", barcodeString);
			intent.putExtra("SOURCE", "com.cats.activity.HomeActivity");
		    startActivity(intent);
        }
		else if (requestCode == REQUEST_CODE_LOGIN && resultCode == RESULT_OK) {
			/**
			 * success log in, go to member page
			 */
			final Intent intent = new Intent(HomeActivity.this, MemberActivity.class);
		    startActivity(intent);
		}
		else if (requestCode == REQUEST_CODE_LOGIN_PICKUP && resultCode == RESULT_OK) {
			final Intent intent = new Intent(HomeActivity.this, PickupActivity.class);
		    startActivity(intent);
		}
		
	}
}
